/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.edu.cliente;

import br.edu.situacao.Situacao;
import br.edu.util.GenericDAO;
import br.edu.util.HibernateUtil;
import java.util.List;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author 'Pedro
 */
public class ClienteDAO extends GenericDAO<Cliente>{
    
    public ClienteDAO(){
        super(Cliente.class);
    }

    public boolean salvar(Cliente cliente) {
        if(cliente.getId()==0){
            adicionar(cliente);
        } else {
            atualizar(cliente);
        }
        return true;
    }

    public List<Cliente> pesquisarNomeLike(String text) {
        List<Cliente> clientes=null;
        setSessao(HibernateUtil.getSessionFactory().openSession());
        setTransacao(getSessao().beginTransaction());
        clientes = (List<Cliente>) getSessao().createCriteria(Cliente.class).add(Restrictions.ilike("nome", text)).list();
        getSessao().close();
        return clientes;
    }

    public Cliente pesquisarCodigo(Integer valueOf) {
        Cliente cliente;
        setSessao(HibernateUtil.getSessionFactory().openSession());
        setTransacao(getSessao().beginTransaction());
        cliente = (Cliente) getSessao().createCriteria(Cliente.class).add(Restrictions.eq("id", valueOf)).uniqueResult();
        getSessao().close();
        return cliente;
    }
}
